package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallActivityShareSpecs;
import com.sc.nft.entity.MallActivityShareTeam;
import com.sc.nft.entity.MallShareTeamOrder;
import com.sc.nft.entity.dto.AdminMallActivityShareTeamPageDTO;
import com.sc.nft.entity.dto.AdminMallShareTeamOrderPageDTO;
import com.sc.nft.entity.dto.MallShareTeamOrderPageDTO;
import com.sc.nft.entity.dto.TeamOrderDTO;
import com.sc.nft.enums.ShareTeamStatusEnum;
import com.sc.nft.mapper.MallActivityShareTeamMapper;
import com.sc.nft.mapper.MallShareTeamOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 分享购活动DAO
 *
 * @author liGen
 * @version 1.0
 * @date 2023/12/11 2:58 下午
 */
@Slf4j
@Repository
public class MallShareTeamOrderDao extends ServiceImpl<MallShareTeamOrderMapper, MallShareTeamOrder> {

    /**
     * 后台成团订单列表
     *
     * @param teamId 团队ID
     * @return
     */
    public Page<AdminMallShareTeamOrderPageDTO> getAdminMallShareTeamOrderPageDTO(Integer pageNo, Integer pageSize, Long teamId) {
        return baseMapper.getAdminMallShareTeamOrderPageDTO(new Page(pageNo, pageSize), teamId);
    }

    /**
     * 成团订单列表
     *
     * @param teamId 团队ID
     * @return
     */
    public Page<MallShareTeamOrderPageDTO> getMallShareTeamOrderPageDTO(Integer pageNo, Integer pageSize, Long teamId) {
        return baseMapper.getMallShareTeamOrderPageDTO(new Page(pageNo, pageSize), teamId);
    }

    /**
     * 获取订单的分享团订单信息
     *
     * @param shareTeamId
     * @return
     */
    public MallShareTeamOrder getByOrderId(Long shareTeamId, Long orderId) {
        LambdaQueryWrapper<MallShareTeamOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallShareTeamOrder::getActivityShareTeamId, shareTeamId);
        wrapper.eq(MallShareTeamOrder::getOrderId, orderId);
        return getOne(wrapper);
    }

    /**
     * 获取是否可以发收益的成团订单
     *
     * @param teamId
     * @param isCanIssue
     * @return
     */
    public List<MallShareTeamOrder> getTeamOrderByIsCanIssue(Long teamId, Boolean isCanIssue) {
        LambdaQueryWrapper<MallShareTeamOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallShareTeamOrder::getActivityShareTeamId, teamId);
        wrapper.eq(MallShareTeamOrder::getIsCanIssue, isCanIssue);
        return list(wrapper);
    }

    /**
     * 获取分享购订单信息
     *
     * @param teamId
     * @return
     */
    public List<TeamOrderDTO> getTeamOrderDTO(Long teamId) {
        return baseMapper.getTeamOrderDTO(teamId);
    }

    /**
     * 修改成团订单状态
     *
     * @param ids
     * @param isIssue
     * @param incomeTime
     * @return
     */
    public boolean updateIsIssue(List<Long> ids, Boolean isIssue, Date incomeTime) {
        UpdateWrapper<MallShareTeamOrder> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        wrapper.set("is_issue", isIssue);
        wrapper.set("income_time", incomeTime);
        return update(new MallShareTeamOrder(), wrapper);
    }
}
